package util;

import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import db.DatabaseManager;
import db.entity.UserAccount;


/**
 * 
 * @author neville
 *
 */
public class Authentication {

	private static DatabaseManager dbM;
	private static List<UserAccount> userList;
	private static String adminID;
	private static boolean isSetup;
	
	/**
	 * Initialize variables for authentication
	 */
	private static void setupAuthentication()
	{
		dbM = new DatabaseManager();
		userList = dbM.listUserAccount();
		adminID = PropertiesLoader.getAdminUser();
		isSetup = true;
	}
	
	/**
	 * To retrieve latest user id 
	 */
	public static void updateUserList()
	{
		setupAuthentication();
	}
	
	/**
	 * Authenticate User ID 
	 * @param inputID	User input to be validate
	 * @return 			Validity of user input
	 */
	public static boolean isUser(String inputID)
	{		
		if(!isSetup)
			setupAuthentication();
			
		for (UserAccount userAct : userList) 
			if(inputID.equals(userAct.getUserID()))	
				return true;
		
		return false;
	}
	
	/** 
	 * Authenticate admin user
	 * @param inputID	User input to be validate
	 * @return			Validity of user input
	 */
	public static boolean isAdmin(String inputID)
	{		
		if(!isSetup)
			setupAuthentication();
		
		if(inputID.equals(adminID))
			return true;
		
		return false;
	}
	
	/**
	 * Display a JOptionPane dialog box to authenticate user
	 * @return			Result of authentication. 
	 * 					If User ID is valid, return User ID.
	 * 					If User ID is invalid, return "Invalid".
	 * 					If user select cancel, return "Exit".
	 */
	public static String authenticateUser()
	{	
		JLabel label = new JLabel("Enter User ID: ");
		JPasswordField pwd = new JPasswordField(20);
		Object options[] = {label, pwd};
		
		int option = JOptionPane.showOptionDialog(null, options, "Authentication Required", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[] {"OK", "Cancel"}, options[0]);
		
		if(option == JOptionPane.NO_OPTION || option == JOptionPane.CLOSED_OPTION)
			return "Exit";
		
		String inputID = (new String(pwd.getPassword())).toUpperCase();
		if(inputID.equals(""))
			return "Invalid";
		
		if(isUser(inputID))
			return inputID;
		
		return "Invalid";
	}
		
	/**
	 * Display a JOptionPane dialog box to authenticate admin
	 * @return			Result of authentication
	 * 					If User ID is valid, return Admin ID.
	 * 					If User ID is invalid, return "Invalid".
	 * 					If user select cancel, return "Exit".
	 */
	public static String authenticateAdmin()
	{		
		JLabel label = new JLabel("Enter Admin ID: ");
		JPasswordField pwd = new JPasswordField(20);
		Object options[] = {label, pwd};
		
		int option = JOptionPane.showOptionDialog(null, options, "Authentication Required", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[] {"OK", "Cancel"}, options[0]);
		
		if(option == JOptionPane.NO_OPTION || option == JOptionPane.CLOSED_OPTION)
			return "Exit";
		
		String inputID = (new String(pwd.getPassword())).toUpperCase();
		if(inputID.equals(""))
			return "Invalid";
	
		if(isAdmin(inputID))
			return inputID;
		
		return "Invalid";
	}
	
	/**
	 * Testing Authentication 
	 */
	public static void main (String [] args)
	{
		String user = authenticateUser();
		if(user == null)
			System.out.println("Canceled");
		else if(user.equals("Invalid"))
			System.out.println("Invalid User");
		else
			System.out.println("Welcome, "+user);
	}	
}

